var express = require('express');
var router = express.Router();
var { goodsModel, shopsModel, loginModel } = require('../../../model/model');



// token key值
var { ACCESS_KEY, REFRESH_KEY } = require('../../API/token_keys/keys')

// 封装创建，验证token
var { generateToken, verifyToken } = require('../../API/generate_token/generateToken')
// token验证 中间件导入
var { short_checkAuth, length_checkAuth } = require('../../API/verify_token/verify_Token')

/* GET home page. */

router.post('/get_shops_list', async (req, res) => {
  let { distance, type, name } = req.body;
  let filter = [];
  let min = 0;
  let max = 0;
  if (distance) {
    min = Number(distance.split('-')[0])
    max = Number(distance.split('-')[1].split('KM')[0]);
    filter.push({ distance: { $gt: min, $lte: max } });
  }
  if (name) filter.push({ shopname: { $regex: name } });
  if (type != '不限' && type != undefined) filter.push({ type });
  let data = await shopsModel.find({ $and: filter });
  res.send({
    code: 200,
    data: data
  });
});

router.get('/get_shop_box', async (req, res) => {
  let { _id } = req.query
  let list = await shopsModel.find({ _id })
  res.send({
    code: 200,
    data: list
  })
})

//获取商店商品列表
router.get('/get_shop_goods', async (req, res) => {
  let { ids } = req.query
  let list = await goodsModel.find({ _id: { $in: ids } })
  res.send({
    code: 200,
    data: list
  })
})

//商品详细列表
router.get('/get_goods_box', short_checkAuth(ACCESS_KEY), async (req, res) => {
  let claim = req.claim
  let { id } = req.query
  let box = await goodsModel.find({ _id: id }).populate('store');
  let user = await loginModel.findOne({ _id: claim._id })
  res.send({
    code: 200,
    data: box,
    tel: user.tel
  })
})


module.exports = router;
